#include <bits/stdc++.h>
using namespace std;
#define yes cout << "YES" << endl
#define no cout << "NO" << endl
using ll = long long;

void solve()
{
    int n;
    cin >> n;
    vector<vector<int>> g(n , vector<int>(n , -1));
    int x = n / 2, y = n / 2;
    if(n%2==0)
        x--, y--;
    int st = 0;
    g[x][y] = st;
    st++;
    int dxy[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; // 右、下、左、上
    ll bc = 1, d = 0;
    while(st<n*n)
    {
        for (int rep = 0; rep < 2;rep++)
        {
            for (int q = 0; q < bc;q++)
            {
                x = x+dxy[d][0], y = y+dxy[d][1];
                if(x<0||x>=n||y<0||y>=n) continue;
                if(g[x][y]==-1)
                g[x][y] = st++;
            }
            d = (d + 1) % 4;
        }
        bc++;
    }
    for (int i = 0; i < n;i++)
    {
        for (int j = 0; j < n;j++)
        {
            cout << g[i][j] << " ";
        }
        cout << endl;
    }
}
int main()
{
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}
